package com.doctor.ui.medicalknowledge;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.hardware.Camera.Area;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import com.doctor.bean.ArticleBean;
import com.doctor.bean.ArticleListBean;
import com.doctor.comm.ConstConfig;
import com.doctor.comm.LoadingTip;
import com.doctor.comm.URLConfig;
import com.doctor.database.DbOperator;
import com.doctor.net.GetDataThread;
import com.doctor.net.NetUtil;
import com.doctor.net.RequestParams;
import com.doctor.ui.BaseActivity;
import com.doctor.ui.BaseFragment;
import com.doctor.ui.R;
import com.doctor.net.GetDataThread.DataCallback;

public class ArticleActivity extends BaseActivity{

	/**
	 * 医学知识文章
	 */
	
	private WebView webView;
	private static String img="<img";
	private static String src="src=\"";
//	private Handler handler;
	private String handledContent;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.article);
		initiaView();
		
//		handler=new Handler(){
//			@Override
//			public void handleMessage(Message msg) {
//				// TODO Auto-generated method stub
//				webView.loadDataWithBaseURL(null, handledContent, "text/html", "UTF-8", null);
//			}
//		};
		
		getData();
	}
	
	
	@Override
	protected void initiaView() {
		// TODO Auto-generated method stub
		webView=(WebView) findViewById(R.id.webView);
		webView.getSettings().setDefaultZoom(android.webkit.WebSettings.ZoomDensity.FAR);
		
//		if (Build.VERSION.SDK_INT<19) {
//			/**
//			 * android4.4以下的版本WebView使用的是WebKit内核，4.4及以上的系统则换成了chromium内核
//			 * chromium取消了字体放大后重排的功能
//			 */
			//下面两个设置实现手指缩放效果
	        webView.getSettings().setSupportZoom(true);
	        webView.getSettings().setBuiltInZoomControls(true);
//		}
        webView.getSettings().setDefaultTextEncodingName("UTF-8") ;
        webView.getSettings().setTextSize(android.webkit.WebSettings.TextSize.LARGER);
	}
	
	@Override
	protected void getData() {
		// TODO Auto-generated method stub
			ArticleBean bean= DbOperator.getInstance(this).selectArticle(getIntent().getIntExtra(ConstConfig.ID, 0));		
			if (bean!=null) {
				
				String content=bean.getContent();
				
				/**
				 * 图片处理策略
				 */
				String[] contentArray=content.split(img);
				StringBuilder builder=new StringBuilder();
				builder.append(contentArray[0]);
				if (contentArray.length>=2) {
					File file=new File(ConstConfig.ARTICLE_IMG_FILE);
					if (!file.exists()) {
						file.mkdirs();
					}
//					Map<File, String> map=new HashMap<File, String>();
					
					for (int i = 1; i < contentArray.length; i++) {
						int srcIndex=contentArray[i].indexOf(src);
						int endIndex=contentArray[i].indexOf("\"", srcIndex+src.length());
						
						String pathAndPic=contentArray[i].substring(srcIndex+src.length(),endIndex);
						int picIndex=pathAndPic.lastIndexOf("/");
//						String path=pathAndPic.substring(pathAndPic.indexOf("/upload")+1,picIndex+1);
						String pic=pathAndPic.substring(picIndex+1, pathAndPic.length());
						contentArray[i]=img+" "+contentArray[i].replaceFirst(pathAndPic, ConstConfig.ARTICLE_IMG_PATH + pic);
						builder.append(contentArray[i]);
						
//						File imgFile=new File(file, pic);
//						if (!imgFile.exists() && NetUtil.hasNetwork(ArticleActivity.this)) {
//							map.put(imgFile, URLConfig.WEBSITE+path+pic);
//						}
					}
					
//					if (map.size()>0) {
//						downLoadImg(map);
//					}					
				}
				StringBuilder buff=new StringBuilder();
				buff.append("<b><body><center><h3><br><font color=\"black\">")
				.append(bean.getTitle())
				.append("</font></h3></center></body></b>")
				.append(builder);
				handledContent=buff.toString();
				webView.loadDataWithBaseURL(null, handledContent, "text/html", "UTF-8", null);
				
			}
	}
	
	
	/**
	 * 下载文章里的图片
	 * @param map
	 */
//	private void downLoadImg(final Map<File, String> map){
//		new Thread(){
//			@Override
//			public void run() {
//				// TODO Auto-generated method stub
//				for (File imgFile : map.keySet()) {
//					//依次下载图片
//					FileOutputStream out = null;
//					InputStream is = null;
//
//					try {
//						URL url=new URL(map.get(imgFile));
//						HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
//
//						imgFile.createNewFile();
//						out = new FileOutputStream(imgFile, true);
//
//						is = httpConnection.getInputStream();
//
//						int length = httpConnection.getContentLength();
//						if (length <= 0) {
//							return;
//						}
//
//						byte[] buff = new byte[1024];
//						int size = 0;
//						int readSize = 0;
//
//						while ((size = is.read(buff)) != -1) {
//							try {
//								out.write(buff, 0, size);
//								readSize += size;
//							} catch (Exception e) {
//								e.printStackTrace();
//							}
//						}
//						
//					} catch (Exception e) {
//						e.printStackTrace();
//					} finally {
//						if (out != null) {
//							try {
//								out.close();
//							} catch (IOException e) {
//								e.printStackTrace();
//							}
//						}
//
//						if (is != null) {
//							try {
//								is.close();
//							} catch (IOException e) {
//								e.printStackTrace();
//							}
//						}
//					}
//				}
//				handler.sendEmptyMessage(0);
//			}
//	}.start();
//	
//	}
	
	
	
	
}
